package com.comunicacion2.model;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public final class CsvHandler {

	public static List<Fingerprint> getFingerprints(String csvFile) {

		List<Fingerprint> fingerprints = new ArrayList<Fingerprint>();

		List<String> csvLines = getTextLines(csvFile);

		if (!csvLines.isEmpty()) {

			for (int i = 1; i < csvLines.size(); i++) {

				String csvLine = csvLines.get(i);
				Fingerprint punto = Fingerprint.fromCsv(csvLine);
				fingerprints.add(punto);
			}
		}
		return fingerprints;
	}

	public static List<String> getTextLines(String textFile) {

		List<String> lines = new ArrayList<String>();

		BufferedReader br = null;

		try {
			br = new BufferedReader(new FileReader(textFile));

			String line = br.readLine();

			while (line != null) {

				lines.add(line);
				line = br.readLine();
			}
			return lines;

		} catch (Exception e) {
			throw new IllegalStateException(e);

		} finally {

			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					throw new IllegalStateException(e);
				}
			}
		}
	}
}
